﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MyUtils.Core
{
    /// <summary>
    /// 时间相关工具
    /// </summary>
    public static class DateTimeUtil
    {
        #region 获取时间戳
        /// <summary> 
        /// 获取时间戳 
        /// </summary>  
        public static string GetTimeStamp(this DateTime dateTime)
        {
            DateTime dtStart = new DateTime(1970, 1, 1, 8, 0, 0);
            return Convert.ToInt64(dateTime.Subtract(dtStart).TotalMilliseconds).ToString();
        }
        #endregion

        #region 根据时间戳获取时间
        /// <summary> 
        /// 根据时间戳获取时间 
        /// </summary>  
        public static DateTime TimeStampToDateTime(this string timeStamp)
        {
            DateTime dtStart = new DateTime(1970, 1, 1, 8, 0, 0);
            return dtStart.AddMilliseconds(Convert.ToInt64(timeStamp));
        }
        #endregion

        #region 本周开始时间
        /// <summary>
        /// 本周开始时间
        /// </summary>
        public static DateTime GetCurrentWeekStart()
        {
            DateTime now = DateTime.Now;
            int day = Convert.ToInt32(now.DayOfWeek.ToString("d"));
            return now.AddDays(1 - day).Date;
        }
        #endregion

        #region 本周结束时间
        /// <summary>
        /// 本周结束时间
        /// </summary>
        public static DateTime GetCurrentWeekEnd()
        {
            return GetCurrentWeekStart().AddDays(7).AddSeconds(-1);
        }
        #endregion

        #region 本月开始时间
        /// <summary>
        /// 本月开始时间
        /// </summary>
        public static DateTime GetCurrentMonthStart()
        {
            DateTime now = DateTime.Now;
            return now.AddDays(1 - now.Day).Date;
        }
        #endregion

        #region 本月结束时间
        /// <summary>
        /// 本月结束时间
        /// </summary>
        public static DateTime GetCurrentMonthEnd()
        {
            return GetCurrentWeekStart().AddMonths(1).AddSeconds(-1);
        }
        #endregion

        #region 本季度开始时间
        /// <summary>
        /// 本季度开始时间
        /// </summary>
        public static DateTime GetCurrentQuarterStart()
        {
            DateTime now = DateTime.Now;
            return now.AddMonths(0 - (now.Month - 1) % 3).AddDays(1 - now.Day).Date;
        }
        #endregion

        #region 本季度结束时间
        /// <summary>
        /// 本季度结束时间
        /// </summary>
        public static DateTime GetCurrentQuarterthEnd()
        {
            return GetCurrentWeekStart().AddMonths(3).AddSeconds(-1);
        }
        #endregion

        #region 本年开始时间
        /// <summary>
        /// 本年开始时间
        /// </summary>
        public static DateTime GetCurrentYearStart()
        {
            return new DateTime(DateTime.Now.Year, 1, 1);
        }
        #endregion

        #region 本年结束时间
        /// <summary>
        /// 本年结束时间
        /// </summary>
        public static DateTime GetCurrentYearEnd()
        {
            return new DateTime(DateTime.Now.Year, 12, 31, 23, 59, 59);
        }
        #endregion

        #region 获取当天的结束时间
        /// <summary>
        /// 获取当天的结束时间
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime GetEndTime(this DateTime date)
        {
            return new DateTime(date.Year, date.Month, date.Day, 23, 59, 59);
        }
        #endregion
        #region 时间差
        /// <summary>
        /// 时间差
        /// </summary>
        /// <param name="date1">被减时间</param>
        /// <param name="date2"></param>
        /// <returns>相减的timespan</returns>
        public static TimeSpan DateDiff(this DateTime date1, DateTime date2)
        {
            return date1 - date2;
        }
        #endregion
        
        #region 获取下一天 
        public static DateTime GetNextDay(this DateTime date, int dayNum = 1)
        {
            return date.AddDays(dayNum);
        }
        #endregion

        #region 获取下周
        public static DateTime GetNextWeek(this DateTime date, int weekNum = 1)
        {
            while (weekNum-- > 0)
            {
                date = date.AddDays(7);
            }
            return date;
        }
        #endregion

        #region 获取下周指定的星期几
        public static DateTime GetNextWeek(this DateTime date, DayOfWeek dayOfWeek, int weekNum = 1)
        {
            while (weekNum-- > 0)
            {
                var inputDateDayOfWeek = (int)date.DayOfWeek;
                var inputDayOfWeek = (int)dayOfWeek;
                date = date.AddDays(7 + (inputDateDayOfWeek - inputDayOfWeek));
            }
            return date;
        }
        #endregion
        #region 获取下个月
        public static DateTime GetNextMonth(this DateTime date, int monthNum = 1)
        {
            var monthStart = date.AddDays(-date.Day + 1);
            var nextMonth = monthStart.AddMonths(monthNum);
            return nextMonth;
        }
        #endregion

        #region 获取下年
        public static DateTime GetNextYear(this DateTime date, int yearNum = 1)
        {
            var monthStart = date.AddDays(-date.Day + 1);
            var yearStart = monthStart.AddMonths(-monthStart.Month + 1);
            var nextYear = yearStart.AddYears(yearNum);
            return nextYear;
        }
        #endregion

        #region 字符串
        /// <summary>
        /// 多行替换（忽略大小写）
        /// </summary>
        /// <param name="str"></param>
        /// <param name="old"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static string ReplaceAll(this string str, string old, string target)
        {
            Regex regex = new Regex(old, RegexOptions.Multiline);
            return regex.Replace(str, target);
        }

        /// <summary>
        /// 数据库语句模糊查询
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string SqlLike(this string str)
        {
            return string.IsNullOrEmpty(str) ? $"%%": $"%{str}%";
        }
        /// <summary>
        /// 左模糊查询
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string SqlLeftLike (this string str)
        {
            return string.IsNullOrEmpty(str) ? $"%%" : $"%{str}";
        }

        /// <summary>
        /// 右模糊查询
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string SqlRightLike(this string str)
        {
            return string.IsNullOrEmpty(str) ? $"%%" : $"{str}%";
        }

        /// <summary>
        /// 获取字符串中所有的数字
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<int>GetNumber(this string str)
        {
            var matches = Regex.Matches(str, @"\d+");
            return matches.Select(x=>int.Parse(x.Value)).ToList();
        }

        #endregion
    }
}
